package com.hndzyu.yd.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Environment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.hndzyu.yd.R;
import com.hndzyu.yd.utils.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * Created by zz9527 on 2018/11/7.
 */
public class WXQRPopupWindow extends PopupWindow {
    private View contextView;
    private Activity mContext;
    @BindView(R.id.save)
    Button save;
    @BindView(R.id.wx)
    Button wx;
    @BindView(R.id.wxqr)
    ImageView ivEwm;
    @BindView(R.id.ll_qr)
    LinearLayout ll_qr;
    @BindView(R.id.payfinish)
    TextView payfinish;

    public interface payFinishListener{
        void payFinish();
    }
    private payFinishListener listener;

    public void setListener(payFinishListener listener) {
        this.listener = listener;
    }
    public WXQRPopupWindow(final Activity context, String url) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contextView = inflater.inflate(R.layout.pop_wxqr, null);
        mContext = context;
        setContentView(contextView);
        ButterKnife.bind(this, contextView);
        ivEwm.setImageBitmap(Util.encodeAsBitmap(url));
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Util.saveBitmap(context,getBitmapByView(ll_qr), "/Screenshot_wxqr.jpg");
                Toast.makeText(context, "保存成功", Toast.LENGTH_SHORT).show();
            }
        });
        wx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Wechat.ShareParams sp = new Wechat.ShareParams();
                sp.setImagePath(Environment.getExternalStorageDirectory()+ "/test/Screenshot_wxqr.jpg");
                sp.setShareType(Platform.SHARE_IMAGE);
                Platform weChat = ShareSDK.getPlatform(Wechat.NAME);
                weChat.share(sp);
            }
        });
        payfinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    listener.payFinish();
            }
        });
        this.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setOutsideTouchable(false);
        this.setFocusable(false);
        this.setTouchable(true);//这个控制PopupWindow内部控件的点击事件
        this.update();



        // 实例化一个ColorDrawable颜色为半透明
//        ColorDrawable dw = new ColorDrawable(30000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
//        this.setBackgroundDrawable(null);
        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.my_dialog);
    }

    public void showDown(View view) {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = 0.7f;
        mContext.getWindow().setAttributes(lp);
        this.showAtLocation(view, Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = 1f;
        mContext.getWindow().setAttributes(lp);
    }

    private Bitmap getBitmapByView(View headerView) {
        int h = headerView.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(headerView.getWidth(), h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        headerView.draw(canvas);
        return bitmap;
    }

}